package com.kuyun.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kuyun.entity.FunsType;
import com.kuyun.service.FunsTypeService;

@Controller
@RequestMapping(value = "/funsType")
public class FunsTypeController {

	@Resource
	private FunsTypeService funsTypeService;

	// 趣事分类管理
	@RequestMapping(value = "/showFunsType")
	public String showFunsType(HttpSession session, Model model) {
		List<FunsType> funsTypeList = funsTypeService.findAllFunsType();
		Integer i = 1;
		for (FunsType funsType : funsTypeList) {
			funsType.setSequnce(i);
			i++;
		}
		model.addAttribute("funsTypeList", funsTypeList);
		return "funsType";
	}

	// 类型修改界面
	@RequestMapping(value = "/getFunsType", method = RequestMethod.GET)
	public String getFunsType(Integer id, Model model) {
		FunsType funsType = funsTypeService.findFunsType(id);
		model.addAttribute("funsType", funsType);
		return "queryFunsType";
	}

	// 趣事类型修改
	@RequestMapping(value = "/queryFunsType", method = RequestMethod.POST)
	public String queryFunsType(FunsType funsType) {
		boolean flag = funsTypeService.queryFunsType(funsType);
		if (flag) {
			return "redirect:/funsType/showFunsType";
		} else {
			return "404";
		}
	}

	// 类型增加界面
	@RequestMapping(value = "/goFunsType")
	public String getFunsType(Model model) {
		return "insFunsType";
	}

	// 趣事类型增加
	@RequestMapping(value = "/insFunsType", method = RequestMethod.POST)
	public String insFunsType(FunsType funsType) {
		boolean flag = funsTypeService.insFunsType(funsType);
		if (flag) {
			return "redirect:/funsType/showFunsType";
		} else {
			return "404";
		}
	}

	// 趣事类型删除
	@RequestMapping(value = "/delFunsType", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> delFunsType(Integer funsTypeId) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		boolean flag = funsTypeService.delFunsType(funsTypeId);
		if (flag) {
			resultMap.put("result", "true");
		} else {
			resultMap.put("result", "false");
		}
		return resultMap;
	}

	// 表单数据校验
	@RequestMapping(value = "/compareFunsType", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> compareFunsType(String type) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		FunsType funsType = funsTypeService.compareFunsType(type);
		if (funsType == null) {
			resultMap.put("result", "true");
		} else {
			resultMap.put("result", "false");
		}
		return resultMap;
	}

}
